// var pntColor = Math.floor(Math.random() * 8 + 1);
// 6号是红色，4号是黄色，7号是绿色
const pntColor = [7, 90, 4, 6, 6];
const POINT_STYLE = new Zondy.Object.CPointInfo({
  Angle: 0,
  Color: 6,
  Space: 0,
  SymHeight: 5,
  SymID: 21,
  SymWidth: 5,
});
function getPntStyle(level) {
  return new Zondy.Object.CPointInfo({
    Angle: 0,
    Color: pntColor[level - 1],
    Space: 0,
    SymHeight: 5,
    SymID: 21,
    SymWidth: 5,
  });
}
export default class Point {
  static query({ position, service, radius, onSuccess, map }) {
    var queryStruct = new Zondy.Service.QueryFeatureStruct();
    queryStruct.IncludeGeometry = true;
    queryStruct.IncludeWebGraphic = true;
    //创建一个用于查询的点形状
    var pointObj = new Zondy.Object.Point2D(...position);
    //设置点容差半径
    pointObj.nearDis = radius;
    //定义查询规则
    var rule = new Zondy.Service.QueryFeatureRule({
      //是否将要素的可见性计算在内
      EnableDisplayCondition: false,
      //是否完全包含
      MustInside: false,
      //是否仅比较要素的外包矩形
      CompareRectOnly: false,
      //是否相交
      Intersect: true,
    });
    // 实例化查询参数
    var queryParam = new Zondy.Service.QueryParameter({
      geometry: pointObj,
      resultFormat: 'json',
      struct: queryStruct,
      rule,
    });
    const ip = 'localhost';
    const port = 6163;
    var queryService = new Zondy.Service.QueryDocFeature(
      queryParam,
      service.docName,
      service.layerID,
      {
        ip,
        port, //访问IGServer的端口号，.net版为6163，Java版为8089
      }
    );
    if (onSuccess) {
      queryService.query(onSuccess);
      return;
    }
    try {
      queryService.query(this.querySuccess(map));
    } catch (err) {
      console.log('参数有误，无法查询');
    }
  }
  static querySuccess(map) {
    return function (result) {
      console.log(result);
      try {
        var format = new Zondy.Format.PolygonJSON();
        // 将MapGIS要素JSON反序列化为ol对象
        var features = format.read(result);

        //定义数据源来接收查询到的要素
        var drawSource = new ol.source.Vector({
          wrapX: false,
        });
        //添加到数据源里
        drawSource.addFeatures(features);
        var drawLayer = new ol.layer.Vector({
          source: drawSource,
          style: new ol.style.Style({
            //填充色
            fill: new ol.style.Fill({
              color: 'rgba(23, 159, 219, 0.5)',
            }),
            //边线样式
            stroke: new ol.style.Stroke({
              color: 'rgba(23, 159, 219, 1)',
              width: 1,
            }),
            image: new ol.style.Circle({
              radius: 12,
              fill: new ol.style.Fill({
                color: 'rgba(23, 159, 219, 0.5)',
              }),
            }),
          }),
        });
        map.addLayer(drawLayer);
      } catch (err) {}
    };
  }

  /**
   * @param {array} position
   * @param {array} attr [{key:'name',type:'string',value:'武汉'}]
   * @param {object} service {docName,layerID,docLayer,ip(optional),port(optional)}
   * @param {object} pointStyle new Zondy.Object.CPointInfo optional
   */
  static add({ position, attr, service, pointStyle }, callback) {
    //创建一个点形状，描述点形状的几何信息
    var gpoint = new Zondy.Object.GPoint(...position); //createPoint();
    //设置当前点要素的几何信息
    var fGeom = new Zondy.Object.FeatureGeometry({ PntGeom: [gpoint] });
    //随机输出1~8之间的整数,作为新添加的要素的颜色号
    //描述点要素的符号参数信息
    console.log(attr[2].value);
    var pointInfo = getPntStyle(attr[2].value);
    if (pointStyle) {
      pointInfo = pointStyle;
    }
    //设置当前点要素的图形参数信息
    var webGraphicInfo = new Zondy.Object.WebGraphicsInfo({
      InfoType: 1,
      PntInfo: pointInfo,
    });
    //设置添加点要素的属性信息

    var attValue = attr.map((item) => item.value);
    //创建一个要素
    var feature = new Zondy.Object.Feature({
      fGeom: fGeom,
      GraphicInfo: webGraphicInfo,
      AttValue: attValue,
    });
    //设置要素为点要素
    feature.setFType(1);
    //创建一个要素数据集
    var featureSet = new Zondy.Object.FeatureSet();
    featureSet.clear();
    //设置属性结构
    var cAttStruct = new Zondy.Object.CAttStruct({
      FldName: attr.map((item) => item.key),
      FldNumber: attr.length,
      FldType: attr.map((item) => item.type),
    });
    featureSet.AttStruct = cAttStruct;
    //添加要素到要素数据集
    featureSet.addFeature(feature);

    //
    //创建一个编辑服务类
    let ip = service.ip ? service.ip : 'localhost';
    let port = service.port ? service.port : '6163';
    var editService = new Zondy.Service.EditDocFeature(
      service.docName,
      service.layerID,
      {
        ip,
        port, //访问IGServer的端口号，.net版为6163，Java版为8089
      }
    );
    //执行添加点要素功能
    if (callback) {
      editService.add(featureSet, callback, () => {
        console.log('error');
      });
      return;
    }
    editService.add(featureSet, this.onAddSuccess(service.docLayer));
  }
  static onAddSuccess(docLayer) {
    return function (data) {
      if (data.succeed) {
        alert('添加点要素成功！');
        docLayer.refresh();
      } else {
        alert('添加点要素失败！');
      }
    };
  }
}
